import React from "react";
import Router from "next/router";

import { connect } from "react-redux";
import {
  decrementCounter,
  incrementCounter,
} from "../redux/actions/counterActions";
import { Creators as ArticleCreators } from "redux/actions/article";
import { Creators as AuthCreators } from "redux/actions/auth";
import { getCookie } from "redux/api/cookie";
import { END } from "redux-saga";

import Header from "pages-components/Header/Header.js";
import HeaderLinks from "pages-components/Header/HeaderLinks.js";

class App extends React.Component {
  static async getInitialProps({ store, req }) {
    // store.dispatch(ArticleCreators.getListRequest({ page: 1, limit: 5 }));
    // store.dispatch(END);
    // await store.sagaTask.toPromise();
    // const token = getCookie("access_token", req);
    // store.dispatch(AuthCreators.getCheckAuth({ token: token }));
    // store.dispatch(END);
    // await store.sagaTask.toPromise();
  }

  constructor(props) {
    super(props);
  }

  componentDidMount = () => {
    Router.push("/posts");
  };

  render() {
    return (
      <div>
        <Header
          brand="YaK企业数字化转型研究所"
          links={<HeaderLinks dropdownHoverColor="dark" />}
          fixed
          color="info"
        />
        <div>
          <br />
          <br />
          <br />
          <br />
          <button onClick={this.props.incrementCounter}>Increment</button>
          <button onClick={this.props.decrementCounter}>Decrement</button>
          <h1>{this.props.counter}</h1>
          {console.log("this.props.articleList====>", this.props.articleList)}
          {this.props.articleList?.article_list?.map((value) => (
            <p key={value.aid}>title:{value.title}</p>
          ))}
        </div>
      </div>
    );
  }
}

const mapStateToProps = (state) => ({
  counter: state.counter.value,
  articleList: state.articleList.articleList.data,
});

const mapDispatchToProps = {
  incrementCounter: incrementCounter,
  decrementCounter: decrementCounter,
};
export default connect(mapStateToProps, mapDispatchToProps)(App);
